/***************************************************************************
 * Copyright 2012 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the contract ICT-257367.
 *
 * Authors:
 *      Salvatore Piccione (TXT e-solutions SpA)
 *
 * Contributors:
 *        Domenico Rotondi (TXT e-solutions SpA)
 **************************************************************************/
package it.txt.ens.client.subscriber;

import it.txt.ens.client.core.ENSClient;

import java.util.Collection;

/**
 * This interface models a consumer of a specific set of events dispatched by the Even Notification
 * Service.<br/>The actual processing of the events is performed by the registered instances of
 * {@link ENSEventListener}: the method {@link ENSEventListener#onEvent(ENSEvent)}
 * is called every time a new event has been received by the subscriber.
 * 
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT txtgroup.com)
 * @author Domenico Rotondi (TXT e-solutions SpA - domenico.rotondi AT txtgroup.com)
 * @see ENSEventListener
 */
public interface ENSSubscriber extends ENSClient{

    /**
     * Starts the subscription to a specific branch, subset or node of a specific namespace.
     * 
     * @throws IllegalStateException if this instance is already subscribed
     * @throws ENSSubscriptionException if an error occurs while starting the subscription
     */
    public void subscribe () throws ENSSubscriptionException, IllegalStateException;
    
    /**
     * Ends the subscription.<br/>After calling this method, the registered event listeners
     * will no longer receive events.
     * 
     * @throws IllegalStateException if this instance was not subscribed
     * @throws ENSSubscriptionException if an error occurs while ending the subscription
     */
    public void unsubscribe () throws IllegalStateException, ENSSubscriptionException;
    
    /**
     * States whether the subscription is active.
     * 
     * @return <code>true</code> if the subscription is active, <code>false</code> otherwise.
     */
    public boolean isSubscribed ();
    
    /**
     * Adds an event listener.
     * 
     * @param listener an event listener.
     * @return the ID of the event listener. This is the ID that must be used to unregister
     * the given event listener.
     * @throws IllegalArgumentException if <code>listener</code> is <code>null</code>
     */
    public String registerEventListener (ENSEventListener listener) throws IllegalArgumentException;
    
    /**
     * Removes the event listener identified by <code>listenerID</code>
     * 
     * @param listenerID the ID of the event listener to delete
     * @throws IllegalArgumentException if <code>listenerID</code> is <code>null</code>
     */
    public void unregisteredEventListener (String listenerID) throws IllegalArgumentException;
    
    /**
     * Removes a set of event listeners.
     * 
     * @param listenerIDs the IDs of the event listeners to delete
     * @throws IllegalArgumentException if <code>listenerIDs</code> is <code>null</code> or is empty
     */
    public void unregisterEventListeners (String ... listenerIDs) throws IllegalArgumentException;
    
    /**
     * Removes a collection of event listeners.
     * 
     * @param listenerIDs the IDs of the event listeners to delete
     * @throws IllegalArgumentException if <code>listenerIDs</code> is <code>null</code> or is empty
     */
    public void unregisterEventListeners (Collection<String> listenerIDs) throws IllegalArgumentException;
}
